
# delim ;  
clear all;
set matsize 11000 ;
eststo clear ;
capture program drop add_lab_FE;
capture log close ; 
log using "../logs/universe_composition.log", text replace;

*-------------------------****** OUTPUT SETTINGS *****---------------------;

*settings some settings that are common for the set of plots on this dofile; 
local pdf_plot_settings = 	`"scale(*1.5) ymtick(##4) "';

*settings for latex tables ;
**NOTE: This table has and additional line compared to the other latex preheading
 ( \bigskip\\[6ex])
 \setlength{\tabcolsep}{1.2pt}
 ;

		global prehead_start "prehead(\begin{table}[ht]\centering 
		\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}  
		\caption{@title}
		\setlength{\tabcolsep}{3pt}";
		local font_size "";
		
		global prehead_end         "\begin{tabular}{l*{@span}{c}}\hline\hline)";
		global prehead_end_bigskip "\begin{tabular}{l*{@span}{c}}\hline\hline \bigskip\\[6ex])";
		
		global postfoot "postfoot(\hline\hline
	\multicolumn{@span}{l}{ \footnotesize @note  }\\
		\multicolumn{@span}{l}{ \footnotesize  @starlegend.    }
		\end{tabular}	\end{table})";
		
		
		
		
		
			local tex_settigns  = `"
		${prehead_start}
		`font_size'
		${prehead_end}
		${postfoot}
		"';	

global SE_note "Standard Errors in Parentheses Clustered at the State Level"; 

*----------------------------------------------------------------------------------;

use "../data/state_year_all_80_15.dta", clear  ;

*---------------------------------------------------;
* - Generate variables and general cleanings ;
*---------------------------------------------------;
do "_programs_universe";
do "_setup_expenditure.do";
do "_cleaning_and_new_variables.do";

gen u_aadt_IH=		u_vmt_IH/u_lane_miles_IH;
gen u_aadt_10000_IH =u_aadt_IH/10000;
*EXTRAS;

sum  state year ppiaco_2010 deflate exp_L_IH_SF12a_r exp_IRI_IH_SF12a_r exp_L_IH_SF12a_r   exp_IH_mtn_SF12a_r exp_IH_mtn_SF12_r;
*fix too long variable name;
rename s_average_grade_seg_constant_IH s_avg_grade_seg_con_IH;
*drop pre-1990 hpms grade observations b/c they are too messy;
replace s_average_grade_seg_IH =cond(year<1990,.,s_average_grade_seg_IH);
*HPMS urban share;
gen urban_share_IH= u_lane_miles_urban/u_lane_miles_IH;
drop if year<1984;
tab year;
drop if exp_L_IH_SF12a_r==.;
drop if exp_L_IH_SF12a_r==0;
drop if exp_L_IH_SF12a_r<0;
tab year;

replace s_average_grade_seg_IH=. if year<1992;

tab year;

*------------------------------------------------;
* loop through composition variables regressions ;
*------------------------------------------------;

label var time "t";
	
local listcontinous "c.time cont Baseline" ;
local listcontinous2 "c.time_+_c.time2 cont_2 " ;
local listdiscrete  "ib2.periods dum Periods" ;

local keep_rule "if 1==1 ";



replace sh_new_lm =. if year==1984;	

eststo clear ;
*make second table with all variables;
*baseline, for reference;
*eststo ,prefix(filter_): reg Du_lane_miles_IH exp_L_IH_SF12a_r,  cluster(state);
*eststo ,prefix(filter_): reg Du_lane_miles_IH exp_L_IH_SF12a_r	c.exp_L_IH_SF12a_r#c.time  time,  cluster(state);
*composition variable: hpms grade;

local i=1;
foreach pair in    "s_average_grade_seg_IH      HPMS_grade HPMS-Grade"
					"sh_elev_10m_s   	         elev       Elevation"
					"sh_h20_10m_s		         nlcd_h2o   NLCD-Water"
					"sh_urb_10m_s 		         nlcd_urb   NLCD-Urban"
					"urban_share_IH 	         hpms_urb   HPMS-Urban"
					"unionization_Mem_Total		         union      Unionization "
					"u_aadt_10000_IH             aadt       AADT"
					"sh_new_lm                   sh_new     New-miles "
					"s_sn_d_IH						sn       	Structural-x-Number"
					"s_share_rig_st_IH			rigid_share  Share-x-rigid "	
			{;
			local comp_var = word("`pair'",1);
			local var_title = word("`pair'",2);
			local col_title = word("`pair'",3);
			gen comp_temp = `comp_var';
			label var comp_temp "\(x\)";
			
			eststo ,prefix(filter_): reghdfe Du_lane_miles_IH exp_L_IH_SF12a_r c.exp_L_IH_SF12a_r#(c.time) c.time
								c.comp_temp c.comp_temp#c.exp_L_IH_SF12a_r c.comp_temp#c.exp_L_IH_SF12a_r#(c.time),  
								abs(state) cluster(state);
			drop comp_temp;
			
			* To add titles to the columns ;
			local tit`i' "`col_title'";
			local i=`i'+1;
			
			di "`comp_var'";
			tab year if e(sample)==1;
			tab state if e(sample)==1;
			pause ;
			
			};
estfe . filter_*, labels( state "State FE");
return list;			
esttab filter_* using "${output}/tables/TableB3_universe_comp.tex", replace
	b(%8.3fc) se(%8.3fc)  sfmt(%8.2fc)
	title("Composition Effects in Construction Costs\label{tab:composition_L}")
	indicate(   `r(indicate_fe)')
	order(   )
	note(${SE_note})
	star(+ 0.10 * 0.05 ** 0.01 *** 0.001)
	 unstack mtitle(
					"\LongHead{15ex}{`tit1'}"
					"\LongHead{15ex}{`tit2'}"
					"\LongHead{15ex}{`tit3'}"
					"\LongHead{15ex}{`tit4'}"
					"\LongHead{15ex}{`tit5'}"
					"\LongHead{15ex}{`tit6'}"
					"\LongHead{15ex}{`tit7'}"
					"\LongHead{15ex}{`tit8'}"
					"\LongHead{15ex}{`tit9'}"
					"\LongHead{15ex}{`tit10'}"
	 		)
	noomitted   nobaselevels nonumbers
	substitute(\_ _ 
	"c.comp_temp" "\(x^0\)" 
	"comp_temp" "\(x^0\)" 
	"-x-" " "
	"\( Y^L \) $\times$ t" 
	"\( Y^L  t \)"
	"\(x\) $\times$ \( Y^L \)"
	" \( Y^L \) \(x\) "
	"\(x\) $\times$ \( Y^L  t \)" 
	"\(Y^L  tx\)"
	"State FE" "\cline{2-11}State FE")
	se  par  r2 label nogaps  drop(_cons)  compress
	obslast stats(N, fmt(%9.0fc))
			${prehead_start}
			${prehead_end_bigskip}
			${postfoot};
	;  
eststo clear ;
	


log close;
exit;
		